<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="关键字" prop="customerName">
        <el-input
          v-model="queryParams.customerKeyWords"
          placeholder="企业名称/法定代表人/所属行业"
          clearable
          @keyup.enter.native="handleComplexQuery"
        />
      </el-form-item>

      <!-- <el-form-item label="企业名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <!-- <el-form-item label="法定代表人" prop="legalLeader">
        <el-input
          v-model="queryParams.legalLeader"
          placeholder="请输入法定代表人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <!-- 经营状态 0 开业、1 注销、2 破产 -->

      <!-- <el-form-item label="所属行业" prop="industry">
        <el-input
          v-model="queryParams.industry"
          placeholder="请输入所属行业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->

      <el-form-item label="经营状态" prop="openState">
        <el-select v-model="queryParams.openState" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.customer_bus_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="所属地区" prop="province">
        <el-select v-model="queryParams.province" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.customer_province"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="录入时间">
        <el-date-picker
          v-model="daterangeInputTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleComplexQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['customer:customerInfo:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['customer:customerInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['customer:customerInfo:remove']"
        >删除</el-button>-->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="customerInfoList"
      :border="true"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="企业名称" align="center" prop="customerName" />
      <el-table-column label="法定代表人" align="center" prop="legalLeader" />
      <el-table-column label="成立日期" align="center" prop="registerDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属行业" align="center" prop="industry" />

      <el-table-column label="所属地区" align="center" prop="province">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_province" :value="scope.row.province" />
        </template>
      </el-table-column>
      <el-table-column label="经营状态" align="center" prop="openState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_bus_status" :value="scope.row.openState" />
        </template>
      </el-table-column>
      <el-table-column label="录入人" align="center" prop="inputUserName" />
      <el-table-column label="营销人" align="center" prop="inputUserName" />

      <el-table-column label="录入时间" align="center" prop="inputTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inputTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['customer:customerInfo:edit']"
          >修改</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['customer:customerInfo:remove']"
          >删除</el-button>-->

          <!-- 客户详细信息 -->
          <el-popover
            placement="left"
            width="800"
            trigger="hover"
            :data="customerInfoList"
            @selection-change="handleSelectionChange"
          >
            <el-descriptions title="客户详细信息" direction="vertical" :column="4" border>
              <el-descriptions-item label="序号" align="center">{{ scope.row.id }}</el-descriptions-item>
              <el-descriptions-item label="企业名称">{{ scope.row.customerName }}</el-descriptions-item>
              <el-descriptions-item label="法定代表人" :span="2">{{scope.row.legalLeader}}</el-descriptions-item>
              <el-descriptions-item label="经营状态码">{{ scope.row.openState }}</el-descriptions-item>
              <el-descriptions-item label="所属地区码">{{ scope.row.province}}</el-descriptions-item>
              <el-descriptions-item label="注册资本(万元)">{{ scope.row.regCapital }}</el-descriptions-item>
              <el-descriptions-item label="所属行业">{{ scope.row.scope }}</el-descriptions-item>
              <el-descriptions-item label="经营范围">{{ scope.row.industry }}</el-descriptions-item>
              <el-descriptions-item label="注册地址">{{ scope.row.regAddr }}</el-descriptions-item>
            </el-descriptions>
            <el-button slot="reference" icon="el-icon-info" type="text" size="mini">详细</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="medium">
        <el-row>
          <el-col span="10">
            <div>
              <el-form-item label="企业名称" prop="customerName">
                <el-input v-model="form.customerName" />
              </el-form-item>
            </div>
          </el-col>
          <el-col span="10">
            <div>
              <el-form-item label="法定代表" prop="legalLeader">
                <el-input v-model="form.legalLeader" />
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="10">
            <div>
              <el-form-item label="成立日期" prop="registerDate">
                <el-date-picker
                  clearable
                  v-model="form.registerDate"
                  type="date"
                  style="width: 100%"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择成立日期"
                ></el-date-picker>
              </el-form-item>
            </div>
          </el-col>
          <el-col span="10">
            <div>
              <el-form-item label="经营状态" prop="openState">
                <el-select v-model="form.openState" placeholder="请选择">
                  <el-option
                    v-for="dict in dict.type.customer_bus_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="10">
            <div>
              <el-form-item label="所属地区" prop="province">
                <el-select v-model="form.province" placeholder="请选择">
                  <el-option
                    v-for="dict in dict.type.customer_province"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col span="10">
            <el-form-item label="所属行业" prop="industry">
              <el-input v-model="form.industry" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="注册资本" style="width:42%" prop="regCapital">
          <el-input v-model="form.regCapital" />
        </el-form-item>
        <el-form-item label="注册地址" prop="regAddr" style="width:83%">
          <el-input v-model="form.regAddr" type="textarea" />
        </el-form-item>

        <el-form-item label="经营范围" prop="scope" style="width:83%">
          <el-input v-model="form.scope" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCustomerInfo,
  listCustomerInfoByComplex,
  getCustomerInfo,
  delCustomerInfo,
  addCustomerInfo,
  updateCustomerInfo
} from "@/api/customer/customerInfo";

export default {
  name: "CustomerInfo",
  dicts: ["customer_province", "customer_bus_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户基本信息表格数据
      customerInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 注册地址时间范围
      daterangeInputTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        legalLeader: null,
        openState: null,
        province: null,
        industry: null,
        inputTime: null,
        customerKeyWords: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerName: [
          { required: true, message: "企业名称不能为空", trigger: "blur" }
        ],
        legalLeader: [
          { required: true, message: "法定代表人不能为空", trigger: "blur" }
        ],
        registerDate: [
          { required: true, message: "成立日期不能为空", trigger: "blur" }
        ],
        openState: [
          {
            required: true,
            message: "经营状态不能为空",
            trigger: "change",
            type: "number"
          }
        ],
        province: [
          {
            required: true,
            message: "所属地区不能为空",
            trigger: "change"
          }
        ],
        industry: [
          { required: true, message: "所属行业不能为空", trigger: "blur" }
        ],
        regCapital: [
          { required: true, message: "注册资金不能为空", trigger: "blur" },
          {
            pattern: /^\d*\.?\d+$/,
            message: "注册资金不能为负数",
            trigger: "blur"
          }
        ],
        regAddr: [
          { required: true, message: "注册地址不能为空", trigger: "blur" }
        ],
        scope: [
          { required: true, message: "经营范围不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户基本信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeInputTime && "" != this.daterangeInputTime) {
        this.queryParams.params["beginInputTime"] = this.daterangeInputTime[0];
        this.queryParams.params["endInputTime"] = this.daterangeInputTime[1];
      }
      listCustomerInfoByComplex(this.queryParams).then(response => {
        this.customerInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerName: null,
        legalLeader: null,
        registerDate: null,
        openState: null,
        province: null,
        regCapital: null,
        industry: null,
        scope: null,
        regAddr: null,
        inputTime: null,
        inputUser: null,
        inputUserName: null
      };
      this.resetForm("form");
    },

    /** 复合搜索 */
    handleComplexQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeInputTime && "" != this.daterangeInputTime) {
        this.queryParams.params["beginInputTime"] = this.daterangeInputTime[0];
        this.queryParams.params["endInputTime"] = this.daterangeInputTime[1];
      }
      listCustomerInfoByComplex(this.queryParams).then(response => {
        this.customerInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeInputTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getCustomerInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomerInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomerInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除客户基本信息编号为"' + ids + '"的数据项？')
        .then(function() {
          return delCustomerInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  }
};
</script>
